import {
  getRedPacket,
  getTotalRedPacket,
  getReadPacketGiveList,
  getDayRedPacket,
} from '../../services/channel/api';


export default {
  namespace: 'redPacketStatistics',

  state: {

    loading: true,

    getRedPacketData: [],

    getTotalRedPacketData: {},

    getDayRedPacketData: {
      list: [],
      pagination:{}
    },

    getReadPacketGiveListData: {
      list: [],
      pagination:{}
    },

  },

  effects: {

    *fetchGetRedPacket({payload ,callback}, { call, put }) {
      const response = yield call(getRedPacket, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {getRedPacketData: response.data},
        });
        if (callback) callback();
      }
    },

    *fetchGetTotalRedPacket({payload ,callback}, { call, put }) {
      const response = yield call(getTotalRedPacket, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {getTotalRedPacketData: response.data},
        });
        if (callback) callback();
      }
    },

    *fetchGetDayRedPacket({payload, callback}, { call, put }) {
      const response = yield call(getDayRedPacket, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            getDayRedPacketData: {
              list: response.data.records,
              pagination:{
                current: response.data.current,
                total: response.data.total,
                size: response.data.size,
              }
            }
          }
        });
        if (callback) callback();
      }
    },

    *fetchGetReadPacketGiveList({payload, callback}, { call, put }) {
      const response = yield call(getReadPacketGiveList, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            getReadPacketGiveListData: {
              list: response.data.records,
              pagination:{
                current: response.data.current,
                total: response.data.total,
                size: response.data.size,
              }
            }
          }
        });
        if (callback) callback();
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },

};
